// tab切换

$('.tabBoxItem').click(function(){
  console.log(1)
  var _index = $(this).index();
  var _this = $(this);
  _this.addClass('tabBoxItemOn').siblings().removeClass('tabBoxItemOn')
  $('.tabCententItem').eq(_index).show().siblings().hide();
})
function tabBack(item){
  console.log(1)
}
// shoppingCarClick向右折叠
$('.shoppingCarClick').click(function() {
  var sideWidth = $('.shoppingCar').width();
  if(sideWidth === 370) {
      $('.shoppingCar').animate({
        width: '35px'
      });
  } else {
    $('.shoppingCar').animate({
        width: '370px'
    });
  }
});
//
$('.shopTextLast').click(function(){
  var shopTextHight = $('.shopText').height()
  if(shopTextHight === 25){
      $('.shopText').animate({
        height:'50px'
      })
    }else{
      $('.shopText').animate({
        height:'25px'
    })}
})


// 同城全选功能
$('.tongchen_checked').click(function(){
  if(this.checked){
    $('.tongchen_list_item').prop('checked',true)
  }else{
    $('.tongchen_list_item').prop('checked',false)

  }
})
// // 代购全选功能
$('.daigou_checked').click(function(){
  if(this.checked){
    $('.daigou_list_item').prop('checked',true)
  }else{
    $('.daigou_list_item').prop('checked',false)
  }
})
/// 购物车计算
// add*****************
$(document).on("click",".shoppingCarTable_item_shop_pro_add",function(){
  // 计算单价
    var _price = $(this).parent().next().find('span').html()
    _price = parseFloat(_price)
    // console.log(_price)
    var _priceNum = parseFloat($(this).next().text())
    _price = accDiv(_price,_priceNum)
    console.log(_price)
    var this_priceNum =  _priceNum + 1;
    $(this).next().text( this_priceNum);
    var result = accMul(this_priceNum,_price)
    $(this).parent().next().find('span').html(result )
    console.log(_price * 1000 * this_priceNum /1000 )
    // 计算同城价格
    var  tongchengPrice = $('.shoppingCarTable_item_tongchen').find('.shoppingCarTable_item_shop_pro_price').find('span')
    var tongchengPriceNum = 0;
    for(var i = 0; i < tongchengPrice.length; i++){
        tongchengPriceNum = tongchengPriceNum + parseFloat(tongchengPrice[i].innerHTML)
    }
    $('.tongchen_zongPrice').html(tongchengPriceNum)
    // 计算代购价格

    var  daigouPrice = $('.shoppingCarTable_item_daigou').find('.shoppingCarTable_item_shop_pro_price').find('span')
    var daigouPriceNum = 0;
    for(var i = 0; i < daigouPrice.length; i++){
        daigouPriceNum = daigouPriceNum + parseFloat(daigouPrice[i].innerHTML)
    }
    $('.daigou_zongPrice').html(daigouPriceNum)
    countGouwuche()
})
// 除法函数
function accDiv(arg1,arg2){
    var t1=0,t2=0,r1,r2;
    try{t1=arg1.toString().split(".")[1].length}catch(e){}
    try{t2=arg2.toString().split(".")[1].length}catch(e){}
    with(Math){
    r1=Number(arg1.toString().replace(".",""))
    r2=Number(arg2.toString().replace(".",""))
    return (r1/r2)*pow(10,t2-t1);
    }
}
Number.prototype.div = function (arg){
return accDiv(this, arg);
}
// 乘法函数
function accMul(arg1,arg2)
{
  var m=0,s1=arg1.toString(),s2=arg2.toString();
  try{m+=s1.split(".")[1].length}catch(e){}
  try{m+=s2.split(".")[1].length}catch(e){}
  return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

Number.prototype.mul = function (arg){
return accMul(arg, this);
}
// remover****************
$(document).on("click",".shoppingCarTable_item_shop_pro_remover",function(){
    var _priceNum = parseFloat($(this).prev().text());
    if(_priceNum === 1){
      var removerNode = $(this).parent().parent().remove();
      wathProject()
    }else{
      $(this).prev().text(_priceNum - 1)
    }
    // 计算单价
    var _price = $(this).parent().next().find('span').html()
    _price = parseFloat(_price);
    _price = accDiv(_price,_priceNum);
    // console.log(_price,_priceNum)
    _priceNum = _priceNum-1;
    var result  = accMul(_priceNum,_price)
    $(this).parent().next().find('span').html( result)

    // 计算同城价格
    var  tongchengPrice = $('.shoppingCarTable_item_tongchen').find('.shoppingCarTable_item_shop_pro_price').find('span')
    var tongchengPriceNum = 0;
    for(var i = 0; i < tongchengPrice.length; i++){
        tongchengPriceNum = tongchengPriceNum + parseFloat(tongchengPrice[i].innerHTML)
    }
    $('.tongchen_zongPrice').html(tongchengPriceNum)
    // 计算代购价格

    var  daigouPrice = $('.shoppingCarTable_item_daigou').find('.shoppingCarTable_item_shop_pro_price').find('span')
    var daigouPriceNum = 0;
    for(var i = 0; i < daigouPrice.length; i++){
        daigouPriceNum = daigouPriceNum + parseFloat(daigouPrice[i].innerHTML)
    }
    $('.daigou_zongPrice').html(daigouPriceNum)
    countGouwuche()

})
// 计算总价
function countGouwuche(){
  var _item  = $('.select_list_item')
  var num = 0
  for(var i =0;i<_item.length;i++){
    if(_item[i].checked){
      // var _item =
      var itemNum = $(_item[i]).parent().next().next().next().next().find('span').html();
      itemNum = parseFloat(itemNum)
      num = itemNum+num
    }
  }
  $('.shoppingCarTable_count_select_money_count').html(num)
}
// countGouwuche()
// 已选择商品
  $(document).on("click",".select_list_item",function(){
    var _item  = $('.select_list_item')
    var num = 0
    for(var i =0;i<_item.length;i++){
      if(_item[i].checked){
        num = num+1
      }
    }
    $('.shoppingCarTable_count_select_num').html(num)
    var _a = $(this).parent().next().next().next().next().find('span').html();
    // console.log(_a)
    countGouwuche()
  })
// 代购全选按钮监听
$('.tongchen_checked').on("click",function(){
  var _item  = $('.select_list_item')
  console.log(_item)
  var num = 0
  for(var i =0;i<_item.length;i++){
    if(_item[i].checked){
      num = num+1
    }
  }
  countGouwuche()
})
//同城全选监听按钮
$('.daigou_checked').click(function(){
  var _item  = $('.select_list_item')
  var num = 0
  for(var i =0;i<_item.length;i++){
    if(_item[i].checked){
      // _item[i].
      num = num+1
    }
  }
  $('.shoppingCarTable_count_select_num').html(num);
  countGouwuche();
})

// 同城订单总价
function tongchenCount(){
  var item = $('.shoppingCarTable_item_tongchen .shoppingCarTable_item_shop_pro_price span')
  var num = 0
  for(var i =0;i<item.length;i++){
      var imteNum = parseFloat(item[i].innerHTML)
      num = num+imteNum;
    }
    $('.tongchen_zongPrice').html(num)

}
tongchenCount()

// 代购价格
function daigouCount(){
  var item = $('.shoppingCarTable_item_daigou .shoppingCarTable_item_shop_pro_price span')
  var num = 0
  for(var i =0;i<item.length;i++){
      var imteNum = parseFloat(item[i].innerHTML)
      num = num+imteNum;
    }
    $('.daigou_zongPrice').html(num)

}
daigouCount()

//监听购物车有多少个商品
function wathProject(){
  var projectNum = $('.shoppingCarTable_item_shop_select').length
  console.log(projectNum)
  $('.shopoingCount').html(projectNum)
}
wathProject()


// 添加商品
$('.ItemMoney_btn').click(function(){
  var pid = $(this).attr('pid');
  var nowIndex = 0;
  var addItems = $('.shoppingCarTable_item_shop_select')
  for(var i =0 ;i < addItems.length;i++){
    var _pid = $(addItems[i]).attr('pid')
    if(pid === _pid){
      // var items = $(addItems[i]).children().eq(3).children().eq(1);
      // items.text(parseInt(items.text())+1);
      nowIndex = i;
    }
  }
  if(nowIndex !== 0){
    var items = $(addItems[nowIndex]).children().eq(3).children().eq(1);
    var items_price_number = parseInt(items.text())
    console.log(items_price_number)
    var items_price = $(addItems[nowIndex]).children().eq(4).children().text();
    items_price = parseFloat(items_price)
    // 单价
    var results = accDiv(items_price,items_price_number)
    // 总价
    var resultNum = parseInt(items.text())+1;
    var resultCount = accMul(results,resultNum)
    $(addItems[nowIndex]).children().eq(4).children().text(resultCount)
    console.log(results)
    console.log(items_price)
    console.log(items_price_number)
    items.text(resultNum);
    nowIndex = 0;
    countGouwuche()

  }else{
    var _price = parseFloat($(this).prev().html())
    console.log(_price)
    var _intro = $(this).parent().prev().text()
    console.log(_intro)
    var _url = $(this).parent().prev().prev().attr('src')
    console.log(_url)
    var biaoshi = $(this).parent().prev().children().first().text()
    biaoshi = stripscript(biaoshi)
    console.log(biaoshi)
    var html = '<ul class="shoppingCarTable_item_shop_select" pid="'+pid+'"><li><input type="checkbox" name="" value="" class="tongchen_list_item select_list_item"></li>  <li><a href="#" class="shoppingCarTable_item_shop_pro"><img src="'+_url+'" alt=""></a></li></li><li><span>'+_intro+'</span></li><li><span class="shoppingCarTable_item_shop_pro_add"></span><span>1</span><span class="shoppingCarTable_item_shop_pro_remover"></span></li><li class="shoppingCarTable_item_shop_pro_price">￥<span>'+_price+'</span></li><ul>'
    console.log(html)
    if( biaoshi === "同城"){
      $('.shoppingCarTable_item_tongchen').append(html)
      tongchenCount()
      wathProject()
    }
  }
  // console.log(nowIndex)

})

// 正常过滤特殊符号
function stripscript(s) {
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）&mdash;—|{}【】‘；：”“'。，、？]")
    var rs = "";
    for (var i = 0; i < s.length; i++) {
      rs = rs+s.substr(i, 1).replace(pattern, '');
    }
    return rs;
}


// 轮播图效果
$(function(){
  $('.lunbotuList_item_item').click(function(){
    var  _this = $(this);
    var _index = _this.index();
    lunbotuList(_index)
  })
})
function lunbotuList(index){
    $('.lunbotuList_item > a').eq(index).show(1000).siblings().hide(1000)
    console.log($('.lunbotuList_item > a').eq(index))
    $('.lunbotuList_item_item').eq(index).addClass('lunbotuList_item_btn_on').siblings().removeClass('lunbotuList_item_btn_on')
}

// 返回顶部
$('.shopoingBackBtn').click(function(event){
    event.stopPropagation()
  $('html,body').animate({scrollTop: '0px'}, 800);
})
